let BasePlayer = require("../base/yuanbaoPlayer");
let util = require("util");

let Player = function (cfg, frontendId, sessionId) {
    BasePlayer.call(this, cfg, frontendId, sessionId);
    this.cards = '';
    this.actionFlag = 0;
    this.multiplierForDealer = 0;
    this.multiplier = 1;
    this.niuniuType = 0;
};

util.inherits(Player, BasePlayer);

Player.prototype.resetInTable = function () {
    BasePlayer.prototype.resetInTable.call(this);
    this.actionFlag = 0;
    this.multiplierForDealer = 0;
    this.multiplier = 1;
    this.cards = '';
    this.niuniuType = 0;
};

Player.prototype.getCards = function () {
    return this.cards;
};

Player.prototype.getDisplayCards = function (table) {
    if (this.cards.length === 0) {
        return this.cards;
    }
    if (table.getDealCardLength() === 4) {
        return this.cards.slice(0, 11);
    } else {
        return this.cards;
    }
};

Player.prototype.setAction = function () {
    this.actionFlag = 1;
};

Player.prototype.clearAction = function () {
    this.actionFlag = 0;
};

Player.prototype.betForDealer = function (multiplier) {
    this.multiplierForDealer = multiplier;
    this.actionFlag = 1;
};

Player.prototype.getMultiplierForDealer = function () {
    return this.multiplierForDealer;
};

Player.prototype.getMultiplierForBet = function () {
    return this.multiplier;
};

Player.prototype.bet = function (multiplier) {
    this.multiplier = multiplier;
    this.actionFlag = 1;
};

Player.prototype.commit = function () {
    this.actionFlag = 1;
};

Player.prototype.start = function (cards, fee) {
    this.actionFlag = 0;
    this.multiplierForDealer = 0;
    this.multiplier = 1;
    this.niuniuType = 0;
    this.cards = cards;
    this.balance = this.balance - fee;
};

Player.prototype.finish = function (score) {
    this.balance = this.balance + score;
};

Player.prototype.pushCards = function (type, uid) {
    if (type === 4) {
        this.push("onNiuniuDealCards", {cards: this.cards.slice(0, 11), uid: uid});
    } else if (type === 1) {
        this.push("onNiuniuDealCards", {cards: this.cards.slice(12), uid: uid});
    } else {
        this.push("onNiuniuDealCards", {cards: this.cards, uid: uid});
    }
};

Player.prototype.getAction = function () {
    return this.actionFlag;
};

Player.prototype.toJSON = function () {
    return {
        account: this.account,
        balance: this.balance,
        isOnline: this.onlineFlag,
        position: this.position,
        readyFlag: this.readyFlag,
        actionFlag: this.actionFlag,
        multiplierForDealer: this.multiplierForDealer,
        multiplier: this.multiplier
    };
};

exports.create = function (cfg, frontendId, sessionId) {
    return new Player(cfg, frontendId, sessionId);
};
